﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecreate.Business;
using Ecreate.Model;
using System.Data;

namespace Ecreate.Web.systemanage.order
{
    public partial class ordercrud : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string type = Request.QueryString["type"];
                string id = Request.QueryString["id"];
                if (type != null && id != null && id != "")
                {
                    string sql = "select OrderID,KickOrderID,SN,Times,SellAddress,SellPhoneNumber,ProductName,Price,CustName,CustPhoneNumber,CustAddress,PayAccount,PayDate,IsCheckName"
                        + " from v_order ";
                    if (type == "i")
                    {
                        sql += "where ID='" + id + "'";
                    }
                    else if (type == "o")
                    {
                        sql += "where OrderID='" + id + "'";
                    }
                    DataTable dt = usercontrol.GG.GetTable(sql);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        DataRow dr = dt.Rows[0];
                        if (dr["OrderID"].ToString() == "")
                        {
                            txtOrderID.Text = id;
                        }
                        else
                        {
                            txtOrderID.Text = dr["OrderID"].ToString();
                        }
                        txtKickOrderID.Text = dr["KickOrderID"].ToString();
                        txtSN.Text = dr["SN"].ToString();
                        txtTimes.Text = dr["Times"].ToString();
                        txtProduct.Text = dr["ProductName"].ToString();
                        txtPrice.Text = dr["Price"].ToString();
                        txtSellAddress.Text = dr["SellAddress"].ToString();
                        txtSellPhoneNumber.Text = dr["SellPhoneNumber"].ToString();
                        txtCustName.Text = dr["CustName"].ToString();
                        txtCustPhoneNumber.Text = dr["CustPhoneNumber"].ToString();
                        txtCustAddress.Text = dr["CustAddress"].ToString();
                        txtPayDate.Text = dr["PayDate"].ToString();
                        txtPayAccount.Text = dr["PayAccount"].ToString();
                        if (dr["IsCheckName"].ToString() == "已确认")
                        {
                            btnCheck.Visible = false;
                        }
                        else
                        {
                            utility.SessionObject so = utility.SessionManage.getSession(this);
                            if (so == null || so.Usertype != Ecreate.Web.utility.LoginUserType.admin || !so.Admin.Group.IsAdmin)
                            {
                                btnCheck.Visible = false;
                            }
                        }
                    }
                }
            }
        }

        protected void btnCheck_Click(object sender, EventArgs e)
        {
            try
            {
                utility.SessionObject so = utility.SessionManage.getSession(this);
                if (so == null || so.Usertype != Ecreate.Web.utility.LoginUserType.admin || !so.Admin.Group.IsAdmin)
                {
                    Alert("当前用户无权进行付款确认操作。", "");
                    return;
                }
                Order o = new Order();
                o.ID = Convert.ToInt32(txtOrderID.Text);
                OrderService service = new OrderService();
                service.Check(o);
                Alert("付款确认成功。", "window.location='/systemanage/order/ordercrud.aspx?type=i&id=" + o.ID + "';");
            }
            catch
            {
                Alert("付款确认失败。", "");
            }
        }

        private void Alert(string message,string otherjs)
        {
            Response.Write("<script type=\"text/javascript\" language=\"javascript\">        alert('" + message + "');　" + otherjs + " </script>");
        }
    }
}
